/*
 * jxSwing Project (2014)
 * 
 * by Denilson Edinaldo Pinto
 * 
 * LGPL
 * Copyright (C) 2014
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */
package org.jxSwing.component.panel;

import java.awt.BorderLayout;
import java.awt.Dimension;

import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.border.TitledBorder;

import org.jxSwing.i18n.I18N;
import org.jxUtils.check.CheckUtil;
import org.jxUtils.exception.MyException;
import javax.swing.JSplitPane;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;

/**
 * @author Denilson Edinaldo Pinto
 * @porject jxSwing
 * @package org.jxSwing.component.panel
 * @date 19/09/2014 - 18:43:26
 */
public final class ExceptionPanel extends JPanel {
	
	private static final long serialVersionUID = -4082667129935478475L;
	
	private Throwable exception;
	
	private JSplitPane spltpnException;
	
	private JScrollPane scrlpnMessage;
	
	private JTextArea txtMessage;
	
	private JScrollPane scrlpnDetails;
	
	private JTextArea txtDetails;
	
	/**
	 * @param exception
	 * @wbp.parser.constructor
	 */
	public ExceptionPanel(final Throwable exception) {
	
		this(exception, 500, 300);
	}
	
	/**
	 * @param exception
	 * @param size
	 */
	public ExceptionPanel(final Throwable exception, final Dimension size) {
	
		this(exception, (int)size.getWidth(), (int)size.getHeight());
	}
	
	/**
	 * @param exception
	 * @param width
	 * @param height
	 */
	public ExceptionPanel(final Throwable exception, final int width, final int height) {
	
		setSize(width, height);
		
		initGUI();
		
		setException(exception);
	}
	
	private void initGUI() {
	
		setLayout(new BorderLayout());
		
		add(getSpltpnException(), BorderLayout.CENTER);
	}
	
	public JSplitPane getSpltpnException() {
	
		if(spltpnException == null) {
			spltpnException = new JSplitPane();
			spltpnException.setOneTouchExpandable(true);
			spltpnException.setOrientation(JSplitPane.VERTICAL_SPLIT);
			spltpnException.setLeftComponent(getScrlpnMessage());
			spltpnException.setRightComponent(getScrlpnDetails());
			spltpnException.addComponentListener(new ComponentAdapter() {
				
				@Override
				public void componentResized(ComponentEvent event) {
					
					spltpnException.setDividerLocation(0.4);
				}
			});
		}
		return spltpnException;
	}
	
	private JScrollPane getScrlpnMessage() {
	
		if(scrlpnMessage == null) {
			scrlpnMessage = new JScrollPane();
			scrlpnMessage.setBorder(new TitledBorder(null, I18N.component().error() + ":", TitledBorder.LEADING, TitledBorder.TOP, null, null));
			scrlpnMessage.setViewportView(getTxtMessage());
		}
		return scrlpnMessage;
	}
	
	private JTextArea getTxtMessage() {
	
		if(txtMessage == null) {
			txtMessage = new JTextArea();
			txtMessage.setLineWrap(true);
			txtMessage.setEditable(false);
			txtMessage.setColumns(40);
			txtMessage.setRows(2);
		}
		return txtMessage;
	}
	
	private JScrollPane getScrlpnDetails() {
	
		if(scrlpnDetails == null) {
			scrlpnDetails = new JScrollPane();
			scrlpnDetails.setBorder(new TitledBorder(null, I18N.component().details() + ":", TitledBorder.LEADING, TitledBorder.TOP, null, null));
			scrlpnDetails.setViewportView(getTxtDetails());
		}
		return scrlpnDetails;
	}
	
	private JTextArea getTxtDetails() {
	
		if(txtDetails == null) {
			txtDetails = new JTextArea();
			txtDetails.setEditable(false);
		}
		return txtDetails;
	}
	
	/**
	 * @return the exception
	 */
	public final Throwable getException() {
	
		return exception;
	}
	
	/**
	 * @param exception
	 *        the exception to set
	 */
	public final void setException(final Throwable exception) {
	
		this.exception = exception;
		
		getTxtMessage().setText(CheckUtil.isNotNullAndNotAbsoluteEmpty(this.exception.getMessage()) ? this.exception.getMessage() : I18N.component().error());
		getTxtDetails().setText(MyException.toString(this.exception));
	}
	
	/*
	 * (non-Javadoc)
	 * 
	 * @see java.awt.Component#setSize(int, int)
	 */
	@Override
	public void setSize(final int width, final int height) {
	
		final Dimension dimension = new Dimension(width, height);
		
		super.setSize(width, height);
		super.setPreferredSize(dimension);
		super.setMinimumSize(dimension);
		super.setMaximumSize(dimension);
	}
}
